package cn.edu.cqvie.netty.bytebuf;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class RefCountedTest {

    static ByteBuf parent = Unpooled.buffer(512);

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            parent.writeByte(i);
        }

        try {
            while (parent.isReadable(16)) {
                ByteBuf derived = parent.readSlice(16);
                derived.retain();
                process(derived);
                System.out.println("parent.refCnt():" + parent.refCnt());
            }
        } finally {
            parent.release();
            System.out.println("parent.refCnt():" + parent.refCnt());
        }


    }

    public static void process(ByteBuf buf) {
        System.out.println("derived.refCnt():" + buf.refCnt());
        buf.release();
        System.out.println("derived.refCnt():" + buf.refCnt());
    }

}
